package com.saicmotor.jobschedulerservice;

import androidx.appcompat.app.AppCompatActivity;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化获取JobScheduler对象实例
        JobScheduler scheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE );

        PersistableBundle extras = new PersistableBundle();
        extras.putString("DATA","data");
        // 创建一个job
        JobInfo jobInfo = new
                JobInfo.Builder(100,new ComponentName(this, JobSchedulerService.class))
                .setRequiresCharging(true)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setExtras(extras).build();

        // 提交任务
        if( scheduler.schedule( jobInfo) <= 0 ) {
            // 异常情况
            // ...
        }
    }
}